import { _decorator, Button, Component, instantiate, Label, Node, Prefab, resources, Sprite, SpriteFrame } from 'cc';
import { LayerBase } from './LayerBase';
import { PetItem } from './PetItem';
import { qualityEnum } from '../Enum';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { Start } from './Start';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('DesPetPanel')
export class DesPetPanel extends LayerBase {

    @property(Label)
    nameLabel: Label;
    @property(Label)
    desLabel: Label;
    @property(Label)
    qualityLabel: Label;
    @property(Label)
    attackLabel: Label;
    @property(Label)
    frequencyLabel: Label;
    @property(Label)
    criticalHitLabel: Label;
    @property(Node)
    putOnOffBtn: Node;
    @property(Node)
    enhanceBtn: Node;
    @property(Node)
    compositeBtn:Node;
    @property(Node)
    specialSkillRoot: Node;
    @property(Node)
    petRoot: Node;
    @property(Prefab)
    petItemPre: Node;
    @property(Node)
    salvageBtn:Node;
    @property(Label)
    baseAttackLabel:Label;
    @property(Label)
    enchanceAttackLabel:Label;

    _data;
    pet;
    _isClickTopContent;
    init(data?: any): void {
        this._data=data;
        this.pet = data.pet;
        this._isClickTopContent=data.isClickTopContent;
        let qualityStr = "普通"
        switch (this.pet.petTp.quality) {
            case qualityEnum.putong:
                qualityStr = "普通"
                break;
            case qualityEnum.xiyou:
                qualityStr = "稀有"
                break;
            case qualityEnum.shishi:
                qualityStr = "史诗"
                break;
            case qualityEnum.chuanshuo:
                qualityStr = "传说"
                break;
        }

        this.qualityLabel.string = "品质：" + qualityStr;
        this.nameLabel.string = this.pet.petTp.name;
        this.desLabel.string = "简介:" + this.pet.petTp.des;
        this.attackLabel.string = "+"+this.pet.attackAddition ;
        this.frequencyLabel.string = this.pet.frequencyAddition + "";
        this.criticalHitLabel.string = "+"+this.pet.criticalHitAddition*100 + "%";

        if (this.pet.petLevel >=2) {
            this.baseAttackLabel.node.parent.active=true;
            this.enchanceAttackLabel.node.parent.active=true;
            this.baseAttackLabel.string = "+" + this.pet.petTp.attack;
            this.enchanceAttackLabel.string = "+" + (this.pet.attackAddition - this.pet.petTp.attack);
        }
        else{
            this.baseAttackLabel.node.parent.active=false;
            this.enchanceAttackLabel.node.parent.active=false;
        }
      
        if (this.pet.petTp.isHasSpecial) {
            this.specialSkillRoot.getChildByName("hasRoot").active = true;
            this.specialSkillRoot.getChildByName("noHasLabel").active = false;
            this.specialSkillRoot.getChildByName("hasRoot").getChildByName("name").getComponent(Label).string = this.pet.petTp.specialSkillName;
            this.specialSkillRoot.getChildByName("hasRoot").getChildByName("des").getComponent(Label).string = this.pet.petTp.specialSkillDes;
            resources.load(`${this.pet.petTp.specialSkillImgPath}/spriteFrame`, SpriteFrame, (err, res) => {
                this.specialSkillRoot.getChildByName("hasRoot").getChildByName("specialCell").getChildByName("sp").getComponent(Sprite).spriteFrame = res
            })
        }
        else {
            this.specialSkillRoot.getChildByName("hasRoot").active = false;
            this.specialSkillRoot.getChildByName("noHasLabel").active = true;
        }

        console.log(this._isClickTopContent,"this._isClickTopContent")
        if (this._isClickTopContent) {
            console.log("点击的是上层")
            this.putOnOffBtn.active = true;
            this.enhanceBtn.active = true;
            this.compositeBtn.active=false;
            this.salvageBtn.active=true;
        }
        else {
            this.putOnOffBtn.active = false;
            this.enhanceBtn.active = false;
            this.salvageBtn.active=false;

            if (UserInfo.hasPetChipArr[this.pet.petType]>=this.pet.petTp.totalChipNum) {
                this.compositeBtn.active = true;
            }
            else{
                this.compositeBtn.active = false;
            }
        }

        if (this.pet.isGoBattle) {
            this.putOnOffBtn.getChildByName("Label").getComponent(Label).string = "取消上阵"
        }
        else {
            this.putOnOffBtn.getChildByName("Label").getComponent(Label).string = "上阵"
        }


        this.petRoot.removeAllChildren();
        let pre = instantiate(this.petItemPre);
        pre.setParent(this.petRoot);
        console.log(this._isClickTopContent,"this._isClickTopContent1111")
        if (this._isClickTopContent) {
            pre.getComponent(PetItem).init(this.pet, true)
        }
        else {
            pre.getComponent(PetItem).init(this.pet, false)
        }
    }

    onclickPutOnOffBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (this.putOnOffBtn.getChildByName("Label").getComponent(Label).string == "上阵") {
            UserInfo.hasPetDic.getValueList().forEach((ele)=>{
                ele.isGoBattle = false;
            })
            UserInfo.hasPetDic.get(this.pet.petId).isGoBattle=true;
        }
        else {
            UserInfo.hasPetDic.get(this.pet.petId).isGoBattle = false;
        }

        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI(this.node.name,this._data);
        UILayerMgr.getIns.resfreshUI("petPanel");
        Start.ins.initHomePanel();
    }

    onclickEnhanceBtn(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        UILayerMgr.getIns.openUILayer("enhancePetPanel",this.pet)
    }

    onclickCompositeBtn() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        let rewardCongfi = UserInfo.petCongfiDic.get(this.pet.petType);
        rewardCongfi.count = 1;
        UILayerMgr.getIns.openUILayer("getRewardPanel", rewardCongfi);

        let newNum = UserInfo.hasPetChipArr[rewardCongfi.petType] - rewardCongfi.totalChipNum;
        UserInfo.hasPetChipArr[rewardCongfi.petType]=newNum;

        StartSceneGameMgr.ins.getPet(this.pet.petType);

        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI(this.node.name, this._data);
        UILayerMgr.getIns.resfreshUI("petPanel");

    }

    onclickSalvageBtn(){
        AudioMgr.ins.playOneShot("audio/clickSalvageBtn",0.8);

        UILayerMgr.getIns.openUILayer("salvagePetPanel",this.pet);
    }

}


